package net.sx.minemodule.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.util.BindingUtil;
import com.kedll.library.util.DividerListItemDecoration;

import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineItemMineAfterBinding;
import net.sx.minemodule.mvp.bean.AfterBean;
import net.sx.minemodule.mvp.bean.AfterGoodsBean;
import net.sx.minemodule.mvp.item.AfterGoodsItem;

import java.util.List;

public class AfterGoodView extends LinearLayout {

    private MineItemMineAfterBinding mBinding;

    private MultiTypeAdapter adapter = new MultiTypeAdapter();

    public AfterGoodView(Context context) {
        this(context, null);
    }

    public AfterGoodView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AfterGoodView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.mine_item_mine_after, this, true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        mBinding.listMineOrder.setLayoutManager(layoutManager);
        mBinding.listMineOrder.setNestedScrollingEnabled(false);
        mBinding.listMineOrder.setHasFixedSize(true);
        mBinding.listMineOrder.addItemDecoration(new DividerListItemDecoration(context, DividerListItemDecoration.VERTICAL_LIST, R.drawable.drawable_line));
        mBinding.listMineOrder.setAdapter(adapter);

    }

    /**
     * basic=基本信息
     * schedule=处理进度 //等待卖家处理|0','等到买家退货给商家|1','等待卖家收到退货|2','等待卖家退款|3','退款成功|4']
     * type=退款类型       //处理类型 0代表退货退款，1代表仅退款
     * Factorysid=商家sid
     * Factorytitle=商家名字
     * after=申请状态 //0代表申请售后 1正在处理 2处理完成 3售后过期
     *
     * @param model
     */
    public void setData(AfterBean model) {
        setStoreName(model.getBasic().getFactorytitle());
        AfterBean.BasicBean basic = model.getBasic();
        List<AfterGoodsBean> dataList = model.getCommoditylist();
        for (AfterGoodsBean bean : dataList) {
            AfterGoodsItem item = bean.createItem(adapter);
            adapter.addItem(item);
        }
        mBinding.tvRightButtonOrderMine.setOnClickListener(v -> {
            if ("0".equals(basic.getAfter())) {
                ARouter.getInstance().build(ARouterPath.PATH_MINE_APPLYAFTERSALE).withString("AfterBean", JSON.toJSONString(model)).
                        navigation();
            } else if ("3".equals(basic.getAfter())) {

                return;
            } else {

            }
        });
        if ("0".equals(basic.getAfter())) {
            mBinding.tvRightButtonOrderMine.setText("申请售后");
            mBinding.tvRightButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
            mBinding.tvRightButtonOrderMine.setTextColor(getResources().getColor(R.color.color_333333));
        } else if ("3".equals(basic.getAfter())) {
            mBinding.tvRightButtonOrderMine.setText("申请售后");
            mBinding.tvRightButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
            mBinding.tvRightButtonOrderMine.setFocusable(false);
            mBinding.tvRightButtonOrderMine.setTextColor(getResources().getColor(R.color.color_333333));
        } else {
            mBinding.tvRightButtonOrderMine.setText("查看详情");
            mBinding.tvRightButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_a541c9);
            mBinding.tvRightButtonOrderMine.setTextColor(getResources().getColor(R.color.color_a541c9));
            if (basic.getType() == 0) {
                setRefund("退货退款");
            } else {
                setRefund("仅退款");
            }
            if (basic.getSchedule() == 0) {

            }
            switch (basic.getSchedule()) {
                case 0:
                    setSchedule("等待卖家处理");
                    break;
                case 1:
                    setSchedule("等待买家退货给商家");
                    break;
                case 2:
                    setSchedule("等待卖家收到退货");
                    break;
                case 3:
                    setSchedule("等待卖家退款");
                    break;
                case 4:
                    setSchedule("退款成功");
                    break;
                default:
                    break;
            }


        }


    }

    /**
     * 设置退款状态
     *
     * @param refund-
     */
    private void setRefund(String refund) {
        mBinding.tvLeftThreeOrderMine.setVisibility(View.VISIBLE);
        mBinding.tvLeftThreeOrderMine.setText(refund);
    }

    /**
     * 设置处理进度
     *
     * @param schedule
     */
    private void setSchedule(String schedule) {
        mBinding.tvLeftScheduleOrderMine.setVisibility(View.VISIBLE);
        mBinding.tvLeftScheduleOrderMine.setText(schedule);

    }

    /**
     * 设置头像
     *
     * @param headUrl
     */
    private void setHeadImage(String headUrl) {
        BindingUtil.loadCircleImage(mBinding.ivStoreImgMineOrder, headUrl, null);
    }


    /**
     * 设置店铺名
     *
     * @param storeName
     */
    private void setStoreName(String storeName) {
        mBinding.tvStoreNameMineOrder.setText(storeName);
    }


}
